package com.example.databasehomework;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

public class Activity_SignUp extends AppCompatActivity {

    private ImageView returnImg;
    private EditText name, psw, psw_sure, email;
    private RadioButton teacher;
    private Button ret, sure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        getViews();
        setListeners();
    }

    public void getViews() {
        returnImg = findViewById(R.id.signup_head_img);
        name = findViewById(R.id.a_num);
        psw = findViewById(R.id.b_num);
        psw_sure = findViewById(R.id.b_num_sure);
        email = findViewById(R.id.c_num);
        ret = findViewById(R.id.returnToHome);
        sure = findViewById(R.id.signUp);
        teacher = findViewById(R.id.d_num_1);
        teacher.setChecked(true);
    }

    public void setListeners() {
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signUp();
            }
        });
    }

    public void signUp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int priority = HttpUnit.STUDENT;
                if (teacher.isChecked()) priority = HttpUnit.TEACHER;
                String password = psw.getText().toString();
                boolean success;
                int pswTheSame;
                if (password.equals(psw_sure.getText().toString())) {
                    success = HttpUnit.addUser(name.getText().toString(), password,
                            priority, email.getText().toString());
                    pswTheSame = 1;
                } else {
                    success = false;
                    pswTheSame = 0;
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (success) {
                            Toast.makeText(getBaseContext(), "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else if (pswTheSame == 0) {
                            Toast.makeText(getBaseContext(), "两次输入密码不一样", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getBaseContext(), "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }).start();
    }

}